//
//  CMJCommonCell.m
//  CMJWeibo
//
//  Created by BH on 15/12/8.
//
//

#import "CMJCommonCell.h"
#import "CMJCommonCheckItem.h"
#import "CMJCommonLabelItem.h"
#import "CMJCommonSwitchItem.h"
#import "CMJCommonArrowItem.h"
#import "CMJBadgeButton.h"
#import "UIImage+MJ.h"
#import "UIView+MJ.h"
#import "CMJCommon.h"

@interface CMJCommonCell ()

/** 标签 */
@property(nonatomic,strong)UILabel *rightLabel;
/** 开关 */
@property(nonatomic,strong)UISwitch *rightSwitch;
/** 箭头 */
@property(nonatomic,strong)UIImageView *rightArrow;
/** 打钩 */
@property(nonatomic,strong)UIImageView *rightCheck;
/** 右边提醒数字 */
@property(nonatomic,strong)CMJBadgeButton *rightBadge;

@property(nonatomic,weak)UITableView *table;

@end

@implementation CMJCommonCell

#pragma mark - 懒加载右边的view
-(UILabel *)rightLabel
{
    if (_rightLabel == nil) {
        self.rightLabel = [[UILabel alloc] init];
        self.rightLabel.textColor = [UIColor lightGrayColor];
        self.rightLabel.font = [UIFont systemFontOfSize:13.f];
    }
    return _rightLabel;
}

-(UISwitch *)rightSwitch
{
    if (_rightSwitch == nil) {
        self.rightSwitch = [[UISwitch alloc] init];
    }
    return _rightSwitch;
}

-(UIImageView *)rightCheck
{
    if (_rightCheck == nil) {
        self.rightCheck = [[UIImageView alloc] initWithImage:[UIImage imageWithName:@"common_icon_checkmark"]];
    }
    return _rightCheck;
}

-(UIImageView *)rightArrow
{
    if (_rightArrow == nil) {
        self.rightArrow = [[UIImageView alloc] initWithImage:[UIImage imageWithName:@"common_icon_arrow"]];
    }
    return _rightArrow;
}

-(CMJBadgeButton *)rightBadge
{
    if (_rightBadge == nil) {
        self.rightBadge = [[CMJBadgeButton alloc] init];
    }
    return _rightBadge;
}

#pragma mark - 初始化
+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"common";
    CMJCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[CMJCommonCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        cell.table = tableView;
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //设置文字
        self.textLabel.font = [UIFont boldSystemFontOfSize:15.f];
        self.textLabel.highlightedTextColor = self.textLabel.textColor;
        self.detailTextLabel.font = [UIFont systemFontOfSize:11.f];
        self.detailTextLabel.textColor = [UIColor lightGrayColor];
        self.detailTextLabel.highlightedTextColor = self.detailTextLabel.textColor;
        
        //设置cell背景
        self.backgroundColor = [UIColor clearColor];
        self.backgroundView = [[UIImageView alloc] init];
        self.selectedBackgroundView = [[UIImageView alloc] init];
    }
    return self;
}

#pragma mark - setter
-(void)setItem:(CMJCommonItem *)item
{
    _item = item;
    //设置图标
    self.imageView.image = [UIImage imageWithName:item.icon];
    
    //设置标题
    self.textLabel.text = item.title;
    self.detailTextLabel.text = item.subttile;
    
    //设置右边View
    [self setupRightView];
}

/**
 *  设置右边显示的控件
 */
-(void)setupRightView
{
    if (self.item.badgeValue) {
        // 显示提醒数字
        self.rightBadge.badgeValue = self.item.badgeValue;
        self.accessoryView = self.rightBadge;
    } else if ([self.item isKindOfClass:[CMJCommonArrowItem class]]) {
        // 显示箭头
        self.accessoryView = self.rightArrow;
    } else if ([self.item isKindOfClass:[CMJCommonSwitchItem class]]) {
        // 显示开关
        self.accessoryView = self.rightSwitch;
    } else if ([self.item isKindOfClass:[CMJCommonLabelItem class]]) {
        // 显示label
        self.accessoryView = self.rightLabel;
        
        // 设置label的文字
        CMJCommonLabelItem *labelItem = (CMJCommonLabelItem *)self.item;
        self.rightLabel.text = labelItem.text;
        
        // 计算尺寸
        self.rightLabel.size = [labelItem.text sizeWithFont:self.rightLabel.font];
    } else if ([self.item isKindOfClass:[CMJCommonCheckItem class]]) {
        // 显示打钩
        CMJCommonCheckItem *checkItem = (CMJCommonCheckItem *)self.item;
        if (checkItem.checked) {
            self.accessoryView = self.rightCheck;
        } else {
            self.accessoryView = nil;
        }
    } else { // 右边不需要显示任何东西
        self.accessoryView = nil;
    }
}

-(void)setIndexPath:(NSIndexPath *)indexPath
{
    _indexPath = indexPath;
    
    //取出背景view
    UIImageView *bg = (UIImageView *)self.backgroundView;
    UIImageView *selectbg = (UIImageView *)self.selectedBackgroundView;
    
    //根据cell显示的具体位置, 来设置背景view显示的图片
    //获得cell这组的总行数
    NSInteger totalRows = [self.table numberOfRowsInSection:indexPath.section];
    
    if (totalRows == 1) {
        //只有1行
        bg.image = [UIImage resizableImage:@"common_card_background"];
        selectbg.image = [UIImage resizableImage:@"common_card_background_highlighted"];
        
    }else if (indexPath.row == 0){
        //首行
        bg.image = [UIImage resizableImage:@"common_card_top_background"];
        selectbg.image = [UIImage resizableImage:@"common_card_top_background_highlighted"];
        
    }else if (indexPath.row == totalRows-1){
        //末行
        bg.image = [UIImage resizableImage:@"common_card_bottom_background"];
        selectbg.image = [UIImage resizableImage:@"common_card_bottom_background_highlighted"];
    
    }else{
        //其他
        bg.image = [UIImage resizableImage:@"common_card_middle_background"];
        selectbg.image = [UIImage resizableImage:@"common_card_middle_background_highlighted"];
    }
}

#pragma mark - 为了拦截frame的设置, 在这里统一设置所有cell的frame
-(void)setFrame:(CGRect)frame
{
    if (!iOS7) {
        frame.origin.x = -10;
        frame.size.width += 20;
    }
    
    [super setFrame:frame];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.detailTextLabel.x = CGRectGetMaxX(self.textLabel.frame) + 10;
}

@end
